<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sign In</title>
    <!-- Bootstrap v3 CSS -->
    <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
    <!--引入vue-->
    <script src="{% static 'js/vue.global.js' %}"></script>
    <script src="{% static 'js/axios.min.js' %}"></script>

    <style>
        .login-container {
            max-width: 400px;
            margin: 100px auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .login-title {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
            font-size: 24px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .btn-signin {
            background-color: #2ecc71;
            border: none;
            color: white;
            padding: 10px;
            border-radius: 20px;
            width: 100%;
            font-size: 16px;
            margin-top: 10px;
        }
        .btn-signin:hover {
            background-color: #27ae60;
            color: white;
        }
        .forgot-password {
            text-align: center;
            margin-top: 15px;
        }
        .forgot-password a {
            color: #bbb;
            text-decoration: none;
        }
        .remember-me {
            color: #666;
        }
    </style>
</head>
<body style="background-color: #f5f5f5;">
    <div class="container" id="app">
        <div class="login-container">
            <h2 class="login-title">Sign In</h2>
            <form id="loginForm" action="#">
                <div class="form-group">
                    <input type="text" v-model="loginForm.userName" class="form-control"  id="userName" placeholder="用户名" required>
                </div>
                <div class="form-group">
                    <input type="password" v-model="loginForm.pwd" class="form-control" id="pwd" placeholder="密码" required>
                </div>
                <div class="checkbox remember-me">
                    <label>
                        <input type="checkbox" v-model="loginForm.rememberMe" id="rememberMe"> Remember me
                    </label>
                </div>
                <button type="submit" class="btn btn-signin" @click="login()">登 录</button>
                <div class="forgot-password">
                    <a href="#">Forgot your password?</a>
                </div>
            </form>
        </div>
    </div>
    <script>
        function getCookie(name) {
                    let cookieValue = null;
                    if (document.cookie && document.cookie !== '') {
                        const cookies = document.cookie.split(';');
                        for (let i = 0; i < cookies.length; i++) {
                            const cookie = cookies[i].trim();
                            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                                break;
                            }
                        }
                     }
                return cookieValue;
        }
        const csrftoken = getCookie('csrftoken');
        const { createApp,reactive } = Vue;
        const app = Vue.createApp({
            setup() {
               const login = () => {

                  console.log('======登录=============',csrftoken);
                  console.log('用户名：',loginForm.userName);
                  console.log('密码：',loginForm.pwd);
                  console.log('是否记住：',loginForm.rememberMe);

                  //axios.post()进行提交服务器，实现如下代码
                  axios.post('/blog/user/login/',JSON.stringify({
                     userName: loginForm.userName,
                     pwd: loginForm.pwd,
                     rememberMe: loginForm.rememberMe,

                  }),{
    headers: {
        'X-CSRFToken': csrftoken,  // 添加CSRF令牌
        'Content-Type': 'application/json'
    },
    withCredentials: true
    }).then(response => {
                     if(response.data.code == 200){
                        alert('登录成功');
                        window.location.href = '/index/';
                     }
                  })



               };
               const loginForm = reactive({
                  userName: '',
                  pwd: '',
                  rememberMe: false,
                  csrf: ''
               })
               return {
                  login,
                  loginForm
               }
            }
        });
        app.mount('#app');
    </script>
</body>
</html>
